package com.alibaba.citrus.cr.mn.order.center.forecastorder.convert;

import com.alibaba.citrus.cr.common.util.DateUtils;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.mnordercenter.model.dto.ForecastOrderBizQueryResponse;
import com.epoch.app.mnordercenter.model.dto.ForecastOrderLineSDO;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoubc
 * @date 2023/6/1 15:46
 */
public class ForecastOrderSDOToForecastOrderBizQueryResponseConvertor {

    public static ForecastOrderBizQueryResponse convert(ForecastOrderSDO forecastOrderSDO) {
        ForecastOrderBizQueryResponse forecastOrderBizQueryResponse = new ForecastOrderBizQueryResponse();
        if(forecastOrderSDO == null){
            return forecastOrderBizQueryResponse;
        }
        forecastOrderBizQueryResponse.setId(forecastOrderSDO.getId());
        forecastOrderBizQueryResponse.setOrderBizId(forecastOrderSDO.getOrderBizId());
        forecastOrderBizQueryResponse.setOuterOrderId(forecastOrderSDO.getOuterOrderId());
        forecastOrderBizQueryResponse.setOrderType(forecastOrderSDO.getOrderType());
        forecastOrderBizQueryResponse.setOrderTypeName(forecastOrderSDO.getOrderTypeName());
        forecastOrderBizQueryResponse.setOrderTag(forecastOrderSDO.getOrderTag());
        forecastOrderBizQueryResponse.setStatus(forecastOrderSDO.getStatus()!=null?forecastOrderSDO.getStatus():null);
        forecastOrderBizQueryResponse.setCustomerCode(forecastOrderSDO.getCustomerCode());
        forecastOrderBizQueryResponse.setCustomerName(forecastOrderSDO.getCustomerName());
        forecastOrderBizQueryResponse.setAddressCode(forecastOrderSDO.getReceiveAddressCode());
        forecastOrderBizQueryResponse.setAddressName(forecastOrderSDO.getReceiveDetailAddress());
        forecastOrderBizQueryResponse.setReceiveDetailAddress(forecastOrderSDO.getReceiveDetailAddress());
        forecastOrderBizQueryResponse.setBisUnitCode(forecastOrderSDO.getBusinessUnitCode());
        forecastOrderBizQueryResponse.setBisUnitName(forecastOrderSDO.getBusinessUnitName());
        forecastOrderBizQueryResponse.setSalesOrganization(forecastOrderSDO.getSalesOrganizationCode());
        forecastOrderBizQueryResponse.setSalesOrganizationName(forecastOrderSDO.getSalesOrganizationName());
        forecastOrderBizQueryResponse.setSalesChannel(forecastOrderSDO.getSalesChannelCode());
        forecastOrderBizQueryResponse.setSalesChannelName(forecastOrderSDO.getSalesChannelName());
        forecastOrderBizQueryResponse.setSalesDepartment(forecastOrderSDO.getSalesDepartmentCode());
        forecastOrderBizQueryResponse.setSalesDepartmentName(forecastOrderSDO.getSalesDepartmentName());
        forecastOrderBizQueryResponse.setChannel(forecastOrderSDO.getChannelCode());
        forecastOrderBizQueryResponse.setChannelName(forecastOrderSDO.getChannelName());
        forecastOrderBizQueryResponse.setBusinessType(forecastOrderSDO.getBusinessType());
        forecastOrderBizQueryResponse.setBusinessTypeName(forecastOrderSDO.getBusinessTypeName());
        forecastOrderBizQueryResponse.setCustomerReferenceNo(forecastOrderSDO.getCustomerReferenceNo());
        if (forecastOrderSDO.getCustomerExpectTDate() != null) {
            forecastOrderBizQueryResponse.setCustomerExpectTDate(DateUtils.toDateStr(forecastOrderSDO.getCustomerExpectTDate(), DateUtils.DATETIME_FORMAT));
        }
        if (forecastOrderSDO.getCustomerPurchaseDate() != null) {
            forecastOrderBizQueryResponse.setCustomerPurchaseDate(DateUtils.toDateStr(forecastOrderSDO.getCustomerPurchaseDate(), DateUtils.DATETIME_FORMAT));
        }
        if (forecastOrderSDO.getVaildBeginTime() != null) {
            forecastOrderBizQueryResponse.setVaildBeginTime(DateUtils.toDateStr(forecastOrderSDO.getVaildBeginTime(), DateUtils.DATETIME_FORMAT));
        }
        forecastOrderBizQueryResponse.setProductGroupCode(forecastOrderSDO.getProductGroupCode());
        forecastOrderBizQueryResponse.setProductGroupName(forecastOrderSDO.getProductGroupName());
        forecastOrderBizQueryResponse.setSalesmanId(forecastOrderSDO.getSalesmanCode());
        forecastOrderBizQueryResponse.setSalesmanName(forecastOrderSDO.getSalesmanName());
        forecastOrderBizQueryResponse.setCreateReason(forecastOrderSDO.getCreateReason());
        forecastOrderBizQueryResponse.setCreateReasonName(forecastOrderSDO.getCreateReasonName());
        forecastOrderBizQueryResponse.setOrderTotalFee(forecastOrderSDO.getOrderTotalFee());
        forecastOrderBizQueryResponse.setTotalPredictAmount(forecastOrderSDO.getTotalPredictAmount());
        if (forecastOrderSDO.getGmtCreate() != null) {
            forecastOrderBizQueryResponse.setGmtCreate(DateUtils.toDateStr(forecastOrderSDO.getGmtCreate(), DateUtils.DATETIME_FORMAT));
        }
        forecastOrderBizQueryResponse.setCreatorId(forecastOrderSDO.getCreatorId());
        forecastOrderBizQueryResponse.setCreatorName(forecastOrderSDO.getCreatorName());
        if (forecastOrderSDO.getGmtModified() != null) {
            forecastOrderBizQueryResponse.setGmtModified(DateUtils.toDateStr(forecastOrderSDO.getGmtModified(), DateUtils.DATETIME_FORMAT));
        }
        forecastOrderBizQueryResponse.setModifierName(forecastOrderSDO.getModifierName());
        forecastOrderBizQueryResponse.setTransportModeCode(forecastOrderSDO.getTransportModeCode());
        forecastOrderBizQueryResponse.setTransportModeName(forecastOrderSDO.getTransportModeName());
        forecastOrderBizQueryResponse.setContact(forecastOrderSDO.getContact());
        forecastOrderBizQueryResponse.setContactPhone(forecastOrderSDO.getContactPhone());
//        forecastOrderBizQueryResponse.setWareHouseCode(forecastOrderSDO.getShippingWarehouseCode());
//        forecastOrderBizQueryResponse.setWareHouseName(forecastOrderSDO.getShippingWarehouseName());
        forecastOrderBizQueryResponse.setShippingWarehouseCode(forecastOrderSDO.getShippingWarehouseCode());
        forecastOrderBizQueryResponse.setShippingWarehouseName(forecastOrderSDO.getShippingWarehouseName());
        forecastOrderBizQueryResponse.setRemark(forecastOrderSDO.getRemark());
        forecastOrderBizQueryResponse.setSyncVersion(forecastOrderSDO.getSyncVersion());

        //累计总金额 总数量
        long totalFee = 0L;
        int quantity = 0;
        List<ForecastOrderLineSDO> list = new ArrayList<>();
        for (com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO line : forecastOrderSDO.getForecastOrderLineSDOList()) {
            ForecastOrderLineSDO forecastOrderLineSDO = new ForecastOrderLineSDO();
            forecastOrderLineSDO.setOrderLineNo(line.getSerialNumber());
            forecastOrderLineSDO.setScItemCode(line.getScItemOutCode());
            forecastOrderLineSDO.setScItemTitle(line.getScItemTitle());
            forecastOrderLineSDO.setQuantity(line.getQuantity());
            forecastOrderLineSDO.setActivityPriceRemark(line.getActivityPriceRemark());
            forecastOrderLineSDO.setDetailRemark(line.getDetailRemark());
            forecastOrderLineSDO.setSaleUnitName(line.getSaleUnit());
            forecastOrderLineSDO.setInventoryUnitName(line.getInventoryUnit());
            forecastOrderLineSDO.setUnitConvert(line.getUnitConvert());
            forecastOrderLineSDO.setUnitPrice(line.getUnitPrice()!=null?String.valueOf(line.getUnitPrice()):null);
            forecastOrderLineSDO.setTotalFee(line.getTotalFee()!=null?String.valueOf(line.getTotalFee()):null);
            forecastOrderLineSDO.setActualTotalReplyAmount(line.getActualTotalReplyAmount()!=null?String.valueOf(line.getActualTotalReplyAmount()):null);
            //forecastOrderLineSDO.setRefuseReasonName(line.getRefuseReason());
            forecastOrderLineSDO.setRefuseReason(line.getRefuseReason());
            //forecastOrderLineSDO.setActivityId(line.getActivityCode());
            forecastOrderLineSDO.setActivityName(line.getActivityName());
            //forecastOrderLineSDO.setActivityLineNo(line.getActivityNumber());
            forecastOrderLineSDO.setActivityCode(line.getActivityCode());
            forecastOrderLineSDO.setActivityNumber(line.getActivityNumber());
            forecastOrderLineSDO.setSerialNumber(line.getSerialNumber());
            list.add(forecastOrderLineSDO);

            if (StringUtils.isNotBlank(line.getQuantity())) {
                totalFee += line.getUnitPrice() * Integer.parseInt(line.getQuantity());
                quantity += Integer.parseInt(line.getQuantity());
            }
        }
        forecastOrderBizQueryResponse.setOrderTotalFee(totalFee);
        forecastOrderBizQueryResponse.setTotalPredictAmount(quantity);
        forecastOrderBizQueryResponse.setForecastOrderLineSDOList(list);
        return forecastOrderBizQueryResponse;
    }
}
